//
//  ImageEdit.m
//  Test_image
//
//  Created by liu wei on 11-11-7.
//  Copyright 2011年 ifeng. All rights reserved.
//

#import "ImageEdit.h"
#import "CLGloble.h"

#define TargetSlope 177.0f/270.0f
#define LengthLeastSlope 100.0f/270.0f
#define WidthLeastSlope 177.0f/150.0f
#define ImageSlope imageSize.height/imageSize.width
#define SmallSize (imageSize.width < 270.0f && imageSize.height < 177.0f)
#define cutImageHeight 177.0f
#define cutImageWidth 270.0f

@implementation ImageEdit


/**
 * 修发图片大小，用户中心头像尺寸(170*170)
 */
+ (UIImage *) imageWithOriginal:(UIImage *)image scaledToSize:(CGSize)newSize{
    newSize.height=image.size.height*(newSize.width/image.size.width);
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return  newImage;
}

// 平铺后裁剪左上角
+ (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize Image:(UIImage *)image{
  
    return [self baseImageBycllipFromTopAndMiddleForSize:targetSize Image:image placeHoldImage:nil Align:0];
}

// 用个貌似都是这个
+ (UIImage *)cutImage:(UIImage *)image targetSize:(CGSize)targetImageSize
{
    
    
    CGSize sourceImageSize = CGSizeMake(image.size.width, image.size.width);
    if(CGSizeEqualToSize(sourceImageSize, targetImageSize))return image;
    // 原图大小至少有一边小于了显示区域的可视范围
    if(sourceImageSize.width < targetImageSize.width || sourceImageSize.height < targetImageSize.height){
        //表示原图尺寸的宽或者高小于目标尺寸，则先等比放大，再切图
        float widthProportion = targetImageSize.width/sourceImageSize.width;
        float heightproportion = targetImageSize.height/sourceImageSize.height;
        CGSize newSize;
        // 以宽为标准放大，高 超出范围
        if(widthProportion * sourceImageSize.height > targetImageSize.height){
            newSize = CGSizeMake(sourceImageSize.width*widthProportion,sourceImageSize.height*widthProportion);
        }else
        // 以高为标准放大，宽 超出范围
        if(heightproportion*sourceImageSize.width > targetImageSize.width){
            newSize = CGSizeMake(sourceImageSize.width*heightproportion,sourceImageSize.height*heightproportion);
        }
        // 横向居中裁剪 -- 作者的原来的意思“应该是”在这里 缩放，下面再裁剪
        UIImage *newImage = [ImageEdit baseImageBycllipFromTopAndMiddleForSize:newSize
                                                                         Image:image
                                                                          placeHoldImage:nil
                                                                         Align:1];
        //截取目标区域大小的图
        UIImage *cutedImage = [ImageEdit baseImageBycllipFromTopAndMiddleForSize:targetImageSize
                                                                           Image:newImage
                                                                           placeHoldImage:nil
                                                                           Align:1];
        return cutedImage;
    }
    // 图片整个都超显示范围了 -- 基本上都是图集列表那超出！ -- 裁剪顶部，丢弃底部
    else if(sourceImageSize.width >= targetImageSize.width && sourceImageSize.height >= targetImageSize.height){
        //表示原图尺寸大于目标尺寸，直接切图
        CGImageRef imageToSplit = image.CGImage;
        // 这个方法和（baseImageBycllipFromTopAndMiddleForSize:Image:Align:0）裁剪顶上一样！
        CGImageRef targetRef = CGImageCreateWithImageInRect(imageToSplit, CGRectMake(0, 0, targetImageSize.width*2,targetImageSize.height*2));
        UIImage *cutedImage = [UIImage imageWithCGImage:targetRef];
        return cutedImage;
    }
    return nil;
}

+ (UIImage *)CutsGraphicliveImageForSize:(CGSize)targetSize
                                   Image:(UIImage *)URLimage
{
    UIImage *sourceImage    = URLimage;          //等待裁减URLimage
    UIImage *newImage       = nil;
//    CGSize  imageSize       = sourceImage.size;         //图片尺寸
//    CGFloat imageWidth      = imageSize.width;          //图片宽度
//    CGFloat imageHeight     = imageSize.height;         //图片高度
//    CGFloat targetWidth     = targetSize.width;         //设定区域宽度
//    CGFloat targetHeight    = targetSize.height;        //设定区域高度
    CGFloat scaleWidth      = 0.0;                      //缩放比例Y
    CGFloat scaleHeight     = 0.0;                      //缩放比例X
    
    scaleHeight = 128*2;
    scaleWidth = 240*2;
    
    UIGraphicsBeginImageContext(targetSize);
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = CGPointMake((targetSize.width - scaleWidth)/2, (targetSize.height - scaleHeight)/2);
    thumbnailRect.size.width = scaleWidth;
    thumbnailRect.size.height = scaleHeight;
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}



+ (UIImage *)scaleImageForSize:(CGSize )targetSize Image:(UIImage *)image{
    
    CGSize imageSize=image.size;
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = CGPointMake(0, 0);
    
    
    if((imageSize.width < targetSize.width && imageSize.height < targetSize.height)){
        return image;

    }
    
    
    if(imageSize.width/imageSize.height>1.f){
        thumbnailRect.size.width=targetSize.width;
        thumbnailRect.size.height=(imageSize.height/imageSize.width)*targetSize.width;
    }else{
        thumbnailRect.size.height=targetSize.height;
        thumbnailRect.size.width=(imageSize.width/imageSize.height)*targetSize.height;
    }
    
    
    UIGraphicsBeginImageContext(thumbnailRect.size);
    
    [image drawInRect:thumbnailRect];
   UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
    
}

// 底层页裁图策略
+ (UIImage *)CutsImageForSize :(CGSize)targetSize
                         Image:(UIImage *)URLimage{
    
    UIImage *sourceImage = URLimage;          //等待裁减URLimage
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;      //图片尺寸
    CGFloat imageWidth = imageSize.width*2;     //图片宽度
    CGFloat imageHeight = imageSize.height*2;   //图片高度
//    CGFloat targetWidth = targetSize.width;   //设定区域宽度 294*2
//    CGFloat targetHeight = targetSize.height; //设定区域高度 174*2
    
    CGFloat scaleWidth = 0.0;                 //缩放比例Y
    CGFloat scaleHeight = 0.0;                //缩放比例X
    
//    NSLog(@"TargetSlope : %f\nImageSlope : %f\nLengthBigSlope : %f\nWidthBigSlope : %f\n", TargetSlope, ImageSlope, LengthLeastSlope, WidthLeastSlope);
    
    //长度与宽度都小于targetSize
    if (SmallSize) {
        scaleHeight = imageHeight;
        scaleWidth = imageWidth;
        
        UIGraphicsBeginImageContext(targetSize);
        CGRect thumbnailRect = CGRectZero;
        thumbnailRect.origin = CGPointMake((targetSize.width - scaleWidth)/2, (targetSize.height - scaleHeight)/2);
        thumbnailRect.size.width= scaleWidth;
        thumbnailRect.size.height = scaleHeight;
        [sourceImage drawInRect:thumbnailRect];
        newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    
    if (ImageSlope > LengthLeastSlope && ImageSlope < WidthLeastSlope) {
        
        if (imageHeight <= imageWidth) {
            
            if (ImageSlope > LengthLeastSlope && ImageSlope < TargetSlope) {
                scaleWidth = cutImageWidth*2;
                scaleHeight = cutImageWidth*(ImageSlope)*2;
                
                UIGraphicsBeginImageContext(targetSize);
                CGRect thumbnailRect = CGRectZero;
                thumbnailRect.origin = CGPointMake(0, (targetSize.height - scaleHeight)/2);
                thumbnailRect.size.width= scaleWidth;
                thumbnailRect.size.height = scaleHeight;
                [sourceImage drawInRect:thumbnailRect];
                newImage = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
              
                return newImage;
            }
            else {
                scaleHeight = cutImageHeight*2;
                scaleWidth = cutImageHeight/(ImageSlope)*2;
                
                UIGraphicsBeginImageContext(targetSize);
                CGRect thumbnailRect = CGRectZero;
                thumbnailRect.origin = CGPointMake((targetSize.width - scaleWidth)/2, 0);
                thumbnailRect.size.width= scaleWidth;
                thumbnailRect.size.height = scaleHeight;
                [sourceImage drawInRect:thumbnailRect];
                newImage = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
              
                return newImage;
            }
            
        }
        
        else {  //(imageHeight <= imageWidth)
            
            if (ImageSlope > TargetSlope && ImageSlope < WidthLeastSlope) {
                scaleHeight = cutImageHeight*2;
                scaleWidth = cutImageHeight/(ImageSlope)*2;
                
                UIGraphicsBeginImageContext(targetSize);
                CGRect thumbnailRect = CGRectZero;
                thumbnailRect.origin = CGPointMake((targetSize.width - scaleWidth)/2, 0);
                thumbnailRect.size.width= scaleWidth;
                thumbnailRect.size.height = scaleHeight;
                [sourceImage drawInRect:thumbnailRect];
                newImage = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
                return newImage;
            }
            
            else {
                
            }
            
        }
        
    }
    
    else {
        if (imageHeight <= imageWidth) {// 横向细长
            scaleHeight = 100*2;
            scaleWidth = 100/(ImageSlope)*2;
            
            UIGraphicsBeginImageContext(targetSize);
            CGRect thumbnailRect = CGRectZero;
            thumbnailRect.origin = CGPointMake((targetSize.width - scaleWidth)/2, (targetSize.height - scaleHeight)/2);
            thumbnailRect.size.width= scaleWidth;
            thumbnailRect.size.height = scaleHeight;
            [sourceImage drawInRect:thumbnailRect];
            newImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            return newImage;
        }
        
        else { // 纵向细长
            scaleWidth = 150*2;
            scaleHeight = 150*(ImageSlope)*2;
            
            UIGraphicsBeginImageContext(targetSize);
            CGRect thumbnailRect = CGRectZero;
            thumbnailRect.origin = CGPointMake((targetSize.width - scaleWidth)/2, 0);
            thumbnailRect.size.width= scaleWidth;
            thumbnailRect.size.height = scaleHeight;
            [sourceImage drawInRect:thumbnailRect];
            newImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            return newImage;
        }
    }
    
    return newImage;
}

// 底层页策略相关
+ (CGRect)ImageForSize:(CGSize)targetSize
                 Image:(UIImage *)URLimage{
  
  UIImage *sourceImage = URLimage;          //等待裁减URLimage
  // UIImage *newImage = nil;
  CGSize imageSize = sourceImage.size;      //图片尺寸
  CGFloat imageWidth = imageSize.width*2;     //图片宽度
  CGFloat imageHeight = imageSize.height*2;   //图片高度
  //  CGFloat targetWidth = targetSize.width;   //设定区域宽度 294*2
  //  CGFloat targetHeight = targetSize.height; //设定区域高度 174*2
  
  CGFloat scaleWidth = 0.0;                 //缩放比例Y
  CGFloat scaleHeight = 0.0;                //缩放比例X
  
  CGRect rect;
  
  //    NSLog(@"TargetSlope : %f\nImageSlope : %f\nLengthBigSlope : %f\nWidthBigSlope : %f\n", TargetSlope, ImageSlope, LengthLeastSlope, WidthLeastSlope);
  
  //长度与宽度都小于targetSize
  if (SmallSize) {
    scaleHeight = imageHeight;
    scaleWidth = imageWidth;
    
    rect.size.height = scaleHeight;
    rect.size.width = scaleWidth;
    return rect;
  }
  
  if (ImageSlope > LengthLeastSlope && ImageSlope < WidthLeastSlope) {
    
    if (imageHeight <= imageWidth) {
      
      if (ImageSlope > LengthLeastSlope && ImageSlope < TargetSlope) {
        scaleWidth = cutImageWidth*2;
        scaleHeight = cutImageWidth*(ImageSlope)*2;
        
        rect.size.height = scaleHeight;
        rect.size.width = scaleWidth;
        return rect;
      }
      else {
        scaleHeight = cutImageHeight*2;
        scaleWidth = cutImageHeight/(ImageSlope)*2;
        
        rect.size.height = scaleHeight;
        rect.size.width = scaleWidth;
        return rect;
      }
      
    }
    
    else {  //(imageHeight <= imageWidth)
      
      if (ImageSlope > TargetSlope && ImageSlope < WidthLeastSlope) {
        scaleHeight = cutImageHeight*2;
        scaleWidth = cutImageHeight/(ImageSlope)*2;
        
        rect.size.height = scaleHeight;
        rect.size.width = scaleWidth;
        return rect;
      }
      
      else {
        
      }
      
    }
    
  }
  
  else {
    if (imageHeight <= imageWidth) {
      scaleHeight = 100*2;
      scaleWidth = 100/(ImageSlope)*2;
      
      rect.size.height = scaleHeight;
      rect.size.width = scaleWidth;
      return rect;
    }
    
    else {
      scaleWidth = 150*2;
      scaleHeight = 150*(ImageSlope)*2;
      
      rect.size.height = scaleHeight;
      rect.size.width = scaleWidth;
      return rect;
    }
  }
  
  return rect;
}

// 平铺后裁剪左上角，或平铺后剧中裁剪，根据参数align决定
+(UIImage *)baseImageBycllipFromTopAndMiddleForSize:(CGSize)targetSize Image:(UIImage *)image placeHoldImage:(UIImage *)placeHoldImage Align:(NSInteger)align{
  
    
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;       //480,270
    CGFloat width = imageSize.width;           //480
    CGFloat height = imageSize.height;         //270
    CGFloat targetWidth = targetSize.width;    //640
    CGFloat targetHeight = targetSize.height;  //314
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    // 放大充满目标区域：对齐左上角的方法 -- UIViewContentModeScaleAspectFit
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        if (widthFactor>  heightFactor){
            scaleFactor = widthFactor; 
        }else{
            scaleFactor = heightFactor;
        }
        scaledWidth= width * scaleFactor;
        scaledHeight = height * scaleFactor;
    }
    UIGraphicsBeginImageContextWithOptions(targetSize, NO, 0.0);
    // 裁剪图的目标frame
    CGRect thumbnailRect = CGRectZero;
    // 左上角 0,0
    thumbnailRect.origin = CGPointMake(0,0);
    thumbnailRect.size.width= scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    // 横向居中
    if (align==1) {
        CGFloat dis=targetSize.width-scaledWidth;
        thumbnailRect.origin.x=dis/2;
        thumbnailRect.origin.y=0;
    }else
    // 横向和纵向都居中
    if(align==2){
        CGFloat disX = targetSize.width-scaledWidth;
        thumbnailRect.origin.x=disX/2;
        CGFloat disY = targetSize.height-scaledHeight;
        thumbnailRect.origin.y=disY/2;
        
    }
    // 切换昼/夜间模式会有bug（如：夜间模式还依然显示白昼的底图。概率虽不会太大但不好），故搁置不用
    //    [placeHoldImage drawInRect:thumbnailRect];
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

// 没有调用，暂留不杀
+ (UIImage*)imageByScalingAndCroppingForSizeDetail:(CGSize)targetSize Image:(UIImage *)image{
 
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat heightFactor = targetHeight / height;
        scaleFactor = heightFactor; 
        scaledWidth= width * scaleFactor;
    }
    UIGraphicsBeginImageContext(targetSize);
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = CGPointZero;
    thumbnailRect.size.width= scaledWidth;
    thumbnailRect.size.height = scaledHeight;
  
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

// 正文底层页大图预览
+ (CGSize)convertFrameWithWidth:(float)newWidth 
                          Height:(float)newHeight
                    TargetWidth:(float)newTargerWidth
                   TargetHeight:(float)newTargetHeight{
    if (newHeight<=newTargetHeight&&newWidth<=newTargerWidth) {
        if (newHeight/newWidth>=newTargetHeight/newTargerWidth) {
            return CGSizeMake(newWidth*newTargetHeight/newHeight, newTargetHeight);
        }else {
            return CGSizeMake(newTargerWidth, newHeight*newTargerWidth/newWidth);
        }
    }
    if (newHeight>newTargetHeight&&newWidth<=newTargerWidth) {
        return CGSizeMake(newWidth*newTargetHeight/newHeight, newTargetHeight);
    }
    if (newHeight<=newTargetHeight&&newWidth>newTargerWidth) {
        return CGSizeMake(newTargerWidth, newHeight*newTargerWidth/newWidth);
    }
    if (newHeight>newTargetHeight&&newWidth>newTargerWidth) {
        
        if (newHeight/newWidth>=newTargetHeight/newTargerWidth) {
            return CGSizeMake(newWidth*newTargetHeight/newHeight, newTargetHeight);
        }else {
            return CGSizeMake(newTargerWidth, newHeight*newTargerWidth/newWidth);
        }
        
              
        
        
    }
    return CGSizeZero;
}



@end
